
import type { SeatType, SeatDatas } from './types';

// 获取数据中的最大行列
export const getMaxRowCol = (seats: SeatType[] = []) => {
  let cols = 100, rows = 100;
  const _rows: number[] = [];
  const _cols: number[] = [];

  if (seats.length) {
    for (const seat of seats) {
      _rows.push(seat.row);
      _cols.push(seat.column);
    }
    rows = Math.max(..._rows);
    cols = Math.max(..._cols);
  }
  return { cols, rows };
}

export const getSeatId = (seat: SeatType, suffix?: string) => {
  return [seat.row, seat.column, suffix].filter(v => v).join('-');
}

export const toJson = (seat: SeatType) => {
  return JSON.stringify(seat);
}

export const getSeat = (seat: string) => {
  return JSON.parse(seat) as SeatType;
}

export const listsTransform = (seats: SeatType[] = []) => {
  return seats.reduce<SeatDatas>((prev, next) => {
    return { ...prev, [getSeatId(next)]: toJson(next) };
  }, {});
}

export const toList = (seats: SeatDatas) => {
  const values = Object.values(seats);
  return values.map(v => getSeat(v));
}

